from django.db import models
from numpy import *


class Turns(models.Model):

	idT = models.IntegerField(primary_key=True)
	idP = models.IntegerField()
	turn_type = models.IntegerField()
	date = models.DateField()
	doping_joola = models.IntegerField()
	doping_spric = models.IntegerField()
	field_fc_value = models.IntegerField()
	field_type = models.IntegerField()
	serv1_id = models.IntegerField()
	serv2_id = models.IntegerField()
	serv3_id = models.IntegerField()
	base_def_id = models.IntegerField()
	hit1_id = models.IntegerField()
	hit2_id = models.IntegerField()
	hit3_id = models.IntegerField()
	final_score = models.IntegerField()
	
	def __unicode__(self):
		return str(self.idP)+"-"+str(self.idT)

class Pandoleros(models.Model):

	idP = models.IntegerField(primary_key=True)
	name = models.CharField(max_length=100)
	default_joola = models.IntegerField()
	default_spric = models.IntegerField()
	num_turn_game = models.IntegerField()
	num_turn_trening = models.IntegerField()	
	num_mazze_game = models.IntegerField()
	num_mazze_trening = models.IntegerField()
	idU = models.IntegerField()

	def __unicode__(self):
		return self.name

#######
#NAPAD#
#######

	def average_score(self, p=None):
		'''Povprecno stevilo mazz oboje/trening/tekma'''

		if p == None:
			return (self.num_mazze_game+self.num_mazze_trening)/float(self.num_turn_trening+self.num_turn_game);
	
		elif p == 'trening':
			a_t = self.num_mazze_trening/float(self.num_turn_trening)
			return a_t;

		elif p == 'tekma':
			a_g = self.num_mazze_game/float(self.num_turn_game)
			return a_g;

		else:
			pass
	


	def fcCount(self, p=None):
		'''stevec fuori campo oboje/trening/tekma'''
		
		t=Turns.objects.filter(idP=self.idP)
				
		if p == 'trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		if p == 'tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		i = 0
		for j in t:
			if j.hit1_id == 0:
				i=i+1;
			if j.hit2_id == 0:
				i=i+1;
			if j.hit3_id == 0:
				i=i+1;
		return i

	
	def fcRatio(self, p=None, g='whole'):
		'''fuori campo na tekmo'''

		i=2
		if g == 'turn':
			i=1

		if p == None:
			t=Turns.objects.filter(idP=self.idP)
			return i*self.fcCount(None)/float(len(t));
				
		elif p == 'trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)
			return i*self.fcCount('trening')/float(len(t));

		elif p == 'tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)
			return i*self.fcCount('game')/float(len(t));


	def totalScoreCount(self, p=None):
		'''vse tocke tekma/trening/oboje'''
		if p == None:
			t=Turns.objects.filter(idP=self.idP)
				
		elif p == 'trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		elif p == 'tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		i = 0
		for j in t:
			i=i+j.final_score

		return i

	def ccoef(self,a,b):
		'''Korelacijski koeficient'''
		from numpy import std
		from numpy import mean
		ma = mean(a)
		mb = mean(b)
		cov = 0
		for i in range (0, len(a)):
			cov = cov + (a[i]-ma)*(b[i]-mb)
			

		return cov/(std(a)*std(b)*len(a))

	def dopingCorCoef(self,p=None,g='both'):
		'''Korelacijski koeficient (ro) trezen/joola/spric/oboje'''

		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)
	
		c = 0;
		scores=[0]
		dopings=[0]
		if g == 'both':
			dopings = [i.doping_spric+i.doping_joola for i in t]

			scores=[]
			for d in dopings:
				sc = [i.final_score for i in t if i.doping_spric+i.doping_joola==d]
				scores.append(average(sc))

		elif g == 'joola':
			dopings = [i.doping_joola for i in t]

			scores=[]
			for d in dopings:
				sc = [i.final_score for i in t if i.doping_joola==d]
				scores.append(average(sc))

			

		elif g == 'spric':
			dopings = [i.doping_spric for i in t]

			scores=[]
			for d in dopings:
				sc = [i.final_score for i in t if i.doping_spric==d]
				scores.append(average(sc))

		return self.ccoef(dopings,scores)



	def servPercentage(self, p=None):
		'''procent uspesnih serv 1,2,3'''
		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		else:
			pass

		servVector = [0,0,0]
		for turn in t:
			servVector[0] += not(turn.serv1_id)
			servVector[1] += not(turn.serv2_id)
			servVector[2] += not(turn.serv3_id)

			
		s=float(sum(servVector))
		servVector[0] = servVector[0]/s
		servVector[1] = servVector[1]/s
		servVector[2] = servVector[2]/s

		return servVector

	def sucessfulServ(self, p=None):
		'''uspel servirati/ni uspel servirati'''
		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		else:
			pass

		serv = 0
		count = 0
		for turn in t:
			if turn.serv1_id >= -2:
				serv=serv+(not turn.serv1_id)
				count = count+1

			if turn.serv2_id >= -2:
				serv=serv+(not turn.serv2_id)
				count = count+1

			if turn.serv3_id >= -2:
				serv=serv+(not turn.serv3_id)
				count = count+1

		return float(serv)/count


	def turnServ(self, p=None):
		'''uspel servirati v turnu'''
		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		else:
			pass

		serv = 0
		count = 0
		for turn in t:
			serv += (turn.serv1_id ==0 or turn.serv2_id ==0 or turn.serv3_id ==0)
			count+=1;		
		
		return float(serv)/count
	
	def turnHit(self, p=None):
		'''naredil tocke v turnu'''
		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		else:
			pass

		hit = 0
		count = 0
		for turn in t:
			hit += (turn.final_score >0)
			count+=1;		
		
		return float(hit)/count


	def unServReason(self, p=None):
		'''razlog neuspesnih serv'''
		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		else:
			pass

		#[zgresil pandolo, out, ulovljen]#
		servVector = [0,0,0]


		for turn in t:
			s=[turn.serv1_id, turn.serv2_id, turn.serv3_id]
			for serv in s:
				if serv > -1024 and not serv == 0:
					servVector[0] += (serv == -1)
					servVector[1] += (serv == -2)
					servVector[2] += (serv > 0)
			
		unCount=sum(servVector);
		return [float(i)/unCount for i in servVector]
	
	
	def hitEventVector(self, p=None):
		#razlog neuspesnih poskusov
		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		else:
			pass

		#[fouri campo, +12-fc, -12, zgresil pandolo, out, ulovljen]#
		hitVector = [0,0,0,0,0,0]

		for turn in t:
			s=[turn.hit1_id, turn.hit2_id, turn.hit3_id]
			for hit in s:
					hitVector[0] += (hit == 0)
					hitVector[1] += (hit == -1)
					hitVector[2] += (hit == -2)
					hitVector[3] += (hit == -3)
					hitVector[4] += (hit == -4)
					hitVector[5] += (hit > 0)
			
		unCount=sum(hitVector);
		return [float(i)/unCount for i in hitVector]
	
	def unHitReason(self, p=None):
		#razlog neuspesnih poskusov#
		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		else:
			pass

		#[zgresil pandolo, out, ulovljen]#
		hitVector = [0,0,0]


		for turn in t:
			s=[turn.hit1_id, turn.hit2_id, turn.hit3_id]
			for hit in s:
				if hit > -1024 and not hit == 0 and not hit == -1 and not hit == -2:
					hitVector[0] += (hit == -3)
					hitVector[1] += (hit == -4)
					hitVector[2] += (hit > 0)
			
		unCount=sum(hitVector);
		return [float(i)/unCount for i in hitVector]

	

		



#########
#OBRAMBA#
#########



	def caughtPanCount(self, p=None):
		'''ulovljeni pandoli oboje/trening/tekma'''
		if p == None:
			t=Turns.objects.exclude(idP=self.idP)
				
		elif p == 'trening':
			#POZOR! excludamo obratno vrednost!
			t=Turns.objects.exclude(turn_type=0)

		elif p == 'tekma':
			#POZOR! excludamo obratno vrednost!
			t=Turns.objects.exclude(turn_type=1)

		else:
			pass

		i = 0
		for j in t:
			i=i+(j.serv1_id==self.idP);
			i=i+(j.serv2_id==self.idP);
			i=i+(j.serv3_id==self.idP);
			i=i+(j.hit1_id==self.idP);
			i=i+(j.hit2_id==self.idP);
			i=i+(j.hit3_id==self.idP);

		return i

	def caughtPanCountOthers(self, p=None):
		'''ulovljeni pandoli od drugih oboje/trening/tekma'''
		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		else:
			pass

		i = 0
		for j in t:
			i=i+(j.serv1_id>0);
			i=i+(j.serv2_id>0);
			i=i+(j.serv3_id>0);
			i=i+(j.hit1_id>0);
			i=i+(j.hit2_id>0);
			i=i+(j.hit3_id>0);

		return i


	def baseHitCountOthers(self, p=None):
		'''izbite baze od drugih oboje/trening/tekma'''
		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		else:
			pass

		i = 0
		for j in t:
			i=i+(j.base_def_id>0);
		return i

	def baseHitCount(self, p=None):
		'''zadetki v bazo oboje/trening/tekma'''
		if p == None:
			t=Turns.objects.exclude(idP=self.idP)
				
		elif p == 'trening':
			#POZOR! excludamo obratno vrednost!
			t=Turns.objects.exclude(turn_type=0)

		elif p == 'tekma':
			#POZOR! excludamo obratno vrednost!
			t=Turns.objects.exclude(turn_type=1)

		else:
			pass

		i = 0
		for j in t:
			i=i+(j.base_def_id==self.idP);
		return i


	def baseHitRatio(self, p=None, g='whole'):
		'''TODO: zadete baze trening/zadete baze tekma, pressure meter'''
		pass

	
	
	

